package com.yuqing.sentimentapp.widget.xrecyclerview.progressindicator.indicator;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.support.v4.content.ContextCompat;

import com.linkface.baselibrary.utils.DensityUtil;
import com.linkface.baselibrary.utils.LogUtil;
import com.yuqing.sentimentapp.QYApplication;
import com.yuqing.sentimentapp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhouyibo on 2018/5/18.
 */

public class CustomLoadIndicator extends BaseIndicatorController {

    Paint radPaint = new Paint();
    Paint bluePaint = new Paint();
    Paint yellowPaint = new Paint();
    static int RED = 2;
    static int BLUE = 1;
    static int YELLOW = 0;
    int backColor = YELLOW;
    float radCirwidth = DensityUtil.dip2px(QYApplication.getAppContext(), 4);
    float blueCirwidth = DensityUtil.dip2px(QYApplication.getAppContext(), 6);
    float yellowCirwidth = DensityUtil.dip2px(QYApplication.getAppContext(), 4);


    float smallCirwidth = DensityUtil.dip2px(QYApplication.getAppContext(), 4);
    float bigCirwidth = DensityUtil.dip2px(QYApplication.getAppContext(), 6);


    PointF rad = new PointF();
    PointF blue = new PointF();
    PointF yellow = new PointF();

    public CustomLoadIndicator() {
        radPaint.setColor(ContextCompat.getColor(QYApplication.getAppContext(), R.color.color_ff8888));
        bluePaint.setColor(ContextCompat.getColor(QYApplication.getAppContext(), R.color.color_63dcff));
        yellowPaint.setColor(ContextCompat.getColor(QYApplication.getAppContext(), R.color.color_ffe95b));
        rad.x = 0;
        rad.y = 0;

        blue.x = 0;
        blue.y = 0;

        yellow.x = 0;
        yellow.y = 0;
    }


    @Override
    public void draw(Canvas canvas) {
        if (backColor == YELLOW) {
            canvas.drawCircle(yellow.x, getHeight() / 2, yellowCirwidth, yellowPaint);
            canvas.drawCircle(rad.x, getHeight() / 2, radCirwidth, radPaint);
            canvas.drawCircle(blue.x, getHeight() / 2, blueCirwidth, bluePaint);
        } else if (backColor == BLUE) {
            canvas.drawCircle(blue.x, getHeight() / 2, blueCirwidth, bluePaint);
            canvas.drawCircle(yellow.x, getHeight() / 2, yellowCirwidth, yellowPaint);
            canvas.drawCircle(rad.x, getHeight() / 2, radCirwidth, radPaint);
        } else {
            canvas.drawCircle(rad.x, getHeight() / 2, radCirwidth, radPaint);
            canvas.drawCircle(blue.x, getHeight() / 2, blueCirwidth, bluePaint);
            canvas.drawCircle(yellow.x, getHeight() / 2, yellowCirwidth, yellowPaint);
        }
    }

    @Override
    public List<Animator> createAnimation() {
        List<Animator> animators = new ArrayList<>();
        ValueAnimator animator = ValueAnimator.ofFloat(0f, 90.0f);
        // 设置作用对象
        //animator.setTarget(red);
        // 设置执行时间(1000ms)
        animator.setDuration(3000);
        animator.addUpdateListener(animation -> {
            // 获取当前值
            Float mValue = (Float) animation.getAnimatedValue();

            if (mValue <= 30) {
                backColor = YELLOW;
                // 设置横向偏移量
                rad.x = (getWidth()  / 2 - smallCirwidth) * (mValue / 30) + smallCirwidth;
                radCirwidth = smallCirwidth + (bigCirwidth-smallCirwidth) * ( mValue / 30);

                blue.x = getWidth()  / 2 + (getWidth()  / 2 - smallCirwidth) * (mValue / 30);
                blueCirwidth = bigCirwidth -  (bigCirwidth-smallCirwidth) * (mValue / 30);

                yellow.x = getWidth()  - smallCirwidth - (getWidth()  - smallCirwidth*2) * (mValue / 30);
                yellowCirwidth = smallCirwidth;
                if (yellow.x<smallCirwidth){
                    LogUtil.e("mValue <= 30  yellow.x:"+yellow.x);
                }

            } else if (mValue <= 60) {
                backColor = BLUE;
                rad.x = getWidth()  / 2 + (getWidth() / 2 - smallCirwidth) * ((mValue - 30) / 30);
                radCirwidth = bigCirwidth -  (bigCirwidth-smallCirwidth) * ((mValue - 30) / 30);

                blue.x = getWidth()  - smallCirwidth - (getWidth()  - smallCirwidth*2) * ((mValue - 30) / 30);

                yellow.x = (getWidth()  / 2 - smallCirwidth) * ((mValue - 30) / 30) + smallCirwidth;
                yellowCirwidth = smallCirwidth + (bigCirwidth-smallCirwidth) * ((mValue - 30) / 30);
                //    LogUtil.e(LogUtil.getLogTag()+"mValue <= 60  yellow.x:"+yellow.x);
            } else {
                backColor = RED;
                rad.x = getWidth()  - smallCirwidth - (getWidth()  - smallCirwidth*2) * ((mValue - 60) / 30);

                blue.x = (getWidth()  / 2 - smallCirwidth) * ((mValue - 60) / 30) + smallCirwidth;
                blueCirwidth = smallCirwidth + (bigCirwidth-smallCirwidth) * ((mValue - 60) / 30);


                yellow.x = getWidth()  / 2 + (getWidth()  / 2 - smallCirwidth) * ((mValue - 60) / 30);
                yellowCirwidth = bigCirwidth -  (bigCirwidth-smallCirwidth) * ((mValue - 60) / 30);
            }

//            if (mValue <= 30) {
//                backColor = YELLOW;
//                // 设置横向偏移量
//                rad.x = (getWidth() / 2 - PixelUtil.dp2px(5)) * (mValue / 30) + PixelUtil.dp2px(5);
//                radCirwidth = PixelUtil.dp2px(5) * (1 + mValue / 30);
//
//                blue.x = getWidth() / 2 + (getWidth() / 2 - PixelUtil.dp2px(5)) * (mValue / 30);
//                blueCirwidth = PixelUtil.dp2px(5) * (2 - mValue / 30);
//
//                yellow.x = getWidth() - PixelUtil.dp2px(5) - (getWidth() - PixelUtil.dp2px(5)) * (mValue / 30);
//            } else if (mValue <= 60) {
//                backColor = BLUE;
//                rad.x = getWidth() / 2 + (getWidth() / 2 - PixelUtil.dp2px(5)) * ((mValue - 30) / 30);
//                radCirwidth = PixelUtil.dp2px(5) * (2 - (mValue - 30) / 30);
//
//                blue.x = getWidth() - PixelUtil.dp2px(5) - (getWidth() - PixelUtil.dp2px(10)) * ((mValue - 30) / 30);
//
//                yellow.x = (getWidth() / 2 - PixelUtil.dp2px(5)) * ((mValue - 30) / 30) + PixelUtil.dp2px(5);
//                yellowCirwidth = PixelUtil.dp2px(5) * (1 + (mValue - 30) / 30);
//            } else {
//                backColor = RED;
//                rad.x = getWidth() - PixelUtil.dp2px(5) - (getWidth() - PixelUtil.dp2px(10)) * ((mValue - 60) / 30);
//
//                blue.x = (getWidth() / 2 - PixelUtil.dp2px(5)) * ((mValue - 60) / 30) + PixelUtil.dp2px(5);
//                blueCirwidth = PixelUtil.dp2px(5) * (1 + (mValue - 60) / 30);
//
//
//                yellow.x = getWidth() / 2 + (getWidth() / 2 - PixelUtil.dp2px(5)) * ((mValue - 60) / 30);
//                yellowCirwidth = PixelUtil.dp2px(5) * (2 - (mValue - 60) / 30);
//            }
            postInvalidate();
        });
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.start();
        animators.add(animator);
        return animators;
    }
}
